package com.qianying.controller.admin;

import com.qianying.comment.CommonResult;
import com.qianying.entity.UserWorkType;
import com.qianying.service.UserWorkTypeService;
import com.qianying.utils.HttpServletRequestUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户与工种关联控制器
 * 作者：check
 */
@RestController
@RequestMapping("/admin/userWorkType")
@Tag(name = "用户与工种关联管理", description = "用户与工种关联关系的核心接口")
public class UserWorkTypeController {

    @Autowired
    private UserWorkTypeService userWorkTypeService;

    /**
     * 新增用户与工种关联
     */
    @PostMapping("/save")
    @Operation(
            summary = "新增用户与工种关联",
            description = "建立用户与工种的关联关系，可包含工种证书",
            parameters = {
                    @Parameter(name = "workTypeId", description = "工种ID", required = true, example = "101"),
                    @Parameter(name = "workCertificate", description = "工种证书（可选）", example = "电工证-123456")
            }
    )
    public CommonResult<?> saveUserWorkType(@RequestBody UserWorkType userWorkType, HttpServletRequest request) {
        Long userId= HttpServletRequestUtils.GetHttpServletRequestUserId(request);
        return userWorkTypeService.saveUserWorkType(userWorkType,userId);
    }

    /**
     * 更新用户与工种关联（主要更新证书）
     */
    @PostMapping("/update")
    @Operation(
            summary = "更新用户与工种关联",
            description = "主要用于更新工种证书信息，需包含ID",
            parameters = {
                    @Parameter(name = "id", description = "关联记录ID", required = true, example = "1"),
                    @Parameter(name = "workCertificate", description = "工种证书（可选）", example = "电工证-654321")
            }
    )
    public CommonResult<?> updateUserWorkType(@RequestBody UserWorkType userWorkType) {
        return userWorkTypeService.updateUserWorkType(userWorkType);
    }

    /**
     * 根据ID删除关联
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "根据ID删除关联",
            description = "删除指定的用户与工种关联记录",
            parameters = {
                    @Parameter(name = "id", description = "关联记录ID", required = true, example = "1")
            }
    )
    public CommonResult<?> removeById(@RequestParam Long id) {
        return userWorkTypeService.removeById(id);
    }

    /**
     * 根据用户ID查询关联的工种列表
     */
    @GetMapping("/getByUserId")
    @Operation(
            summary = "根据用户ID查询关联的工种列表",
            description = "获取指定用户关联的所有工种及证书信息",
            parameters = {
                    @Parameter(name = "userId", description = "用户ID", required = true, example = "1")
            }
    )
    public CommonResult<List<UserWorkType>> getByUserId(@RequestParam Long userId) {
        return userWorkTypeService.getByUserId(userId);
    }
}
